//
// FILE	
// 				main.c
// AUTHOR
//				Filipe Teixeira Campos
// PROPERTY
//				This code is property of Filipe Teixeira Campos.
//				This code has no warranty, use at Your own risk.
//				The intelectual property is of of Filipe Teixeira Campos.
//
// DESC		 
//				this is the entry point of the code
//				it has also the tasks
// - falta int exception

#include <avr/io.h>
#include <avr/delay.h>
#include <avr/signal.h>
#include <inttypes.h>
#include <fum_comm.h>
#include <fum_kernell.h>

//
// FUNCTION	
// 				task 1
// AUTHOR
//				Filipe Teixeira Campos
// DESC		 
//				. task 1 of the kernell
//
void task1(void) {

#ifdef FU_PROT_MODE
	typedef struct 
	{
		int var_a;
		int var_b;
	} TASK1_MEM;
	
	TASK1_MEM  *t1mem = 0x260 + 0x000 ;
#endif

	DDRB |= 0x1|0x2 ;
	
	while(1)
	{
		FG_Lock(1,0);
		FG_tasksleep(1,50);
		FG_UnLock(1,0);
	
		PORTB |= 0x1;
		FG_tasksleep(1,500);
		PORTB |= 0x2;
		FG_tasksleep(1,500);
		PORTB &= ~(0x2|0x1);
		FG_tasksleep(1,500);
	}
}

//
// FUNCTION	
// 				task 2
// AUTHOR
//				Filipe Teixeira Campos
// DESC		 
//				. task 2 of the kernell
//
void task2(void)
{
#ifdef FU_PROT_MODE
	typedef struct 
	{
		int var_a;
		int var_b;
	} TASK2_MEM;
	
	TASK2_MEM  *t1mem = 0x260 + 0x100 ;
#endif

	DDRB |= 0x4|0x8 ;

	while(1)
	{
		FG_tasksleep(2,10);
		FG_Lock(2,0);
		FG_UnLock(2,0);
	
		PORTB |= 0x4;
		FG_tasksleep(2,1000);
		PORTB |= 0x8;
		FG_tasksleep(2,1000);
		PORTB &= ~(0x4|0x8);
		FG_tasksleep(2,1000);
	}
}


//
// FUNCTION	
// 				task 3
// AUTHOR
//				Filipe Teixeira Campos
// DESC		 
//				. task 3 of the kernell
//
void task3(void)
{
#ifdef FU_PROT_MODE
	typedef struct 
	{
		int var_a;
		int var_b;
	} TASK3_MEM;
	
	TASK3_MEM  *t1mem = 0x260 + 0x200 ;
#endif

	// debug
	InitComm();
	
	strcpy(fu_serial.TxBuffer,"\r\n== FUMEGA KERNEL 1.0 ==");
	TxDataCom();
	
	strcpy(fu_serial.TxBuffer,"\r\nFilipe Teixeira Campos");
	TxDataCom();

	strcpy(fu_serial.TxBuffer,"\r\nh-Help");
	TxDataCom();
					
	
	while(1)
	{
		// rx data ?
		if( fu_serial.ucRxBytes ){
		
			switch( PeekChar() )
			{
				case 'h':
				{
					strcpy(fu_serial.TxBuffer,"\r\n====HELP====");
					TxDataCom();				
					
					strcpy(fu_serial.TxBuffer,"\r\nh-Help");
					TxDataCom();
					
					strcpy(fu_serial.TxBuffer,"\r\ns-stats");
					TxDataCom();	
					
					strcpy(fu_serial.TxBuffer,"\r\nm-memory");
					TxDataCom();
				}
				break;
			
				case 'm':
				{
					unsigned short us;
					unsigned char i;
					
					strcpy(fu_serial.TxBuffer,"\r\n====MEM====");
					TxDataCom();				

					strcpy(fu_serial.TxBuffer,"\r\nMEM Kernell ");
					TxDataCom();
					us=sizeof(RT_FUMEGA);
					strcpy(fu_serial.TxBuffer,"=000");
					fu_serial.TxBuffer[1] += us/100; us=us%100;
					fu_serial.TxBuffer[2] += us/10;
					fu_serial.TxBuffer[3] += us%10;
					TxDataCom();		


					for( i=0;i<fumega.numTasks;i++ )
					{
						strcpy(fu_serial.TxBuffer,"\r\nTask ");
						TxDataCom();
						
						strcpy(fu_serial.TxBuffer,"ID=0 St=0");
						fu_serial.TxBuffer[3] += i;
						fu_serial.TxBuffer[8] += fumega.Task[i].ucState;
						TxDataCom();					
					}

				}
				break;			
				
				case 's':
				{
					unsigned short us;
					unsigned char i;
					
					strcpy(fu_serial.TxBuffer,"\r\n====STAT====");
					TxDataCom();				
					
					strcpy(fu_serial.TxBuffer,"\r\nNum Tasks ");
					TxDataCom();
					i=fumega.numTasks;
					strcpy(fu_serial.TxBuffer,"=00");
					fu_serial.TxBuffer[1] += i/10;
					fu_serial.TxBuffer[2] += i%10;
					TxDataCom();
					
					
					strcpy(fu_serial.TxBuffer,"\r\nCpu Free % ");
					TxDataCom();
				
					us=(fumega.CpuFree*100)/(fumega.CpuRun+fumega.CpuFree);
					i=us;					
					strcpy(fu_serial.TxBuffer,"=000");
					fu_serial.TxBuffer[1] += i/100; i%=100;
					fu_serial.TxBuffer[2] += i/10;
					fu_serial.TxBuffer[3] += i%10;
					TxDataCom();	
				}
				break;						
			}
		}
		
		FG_tasksleep(3,50);
	}

}



//
// FUNCTION	
// 				main
// DESC		: 
//				. main loop
//
void main (void)
{	
#ifdef FU_PROT_MODE
	sbi( MCUCR , SRE | SRW  );
#endif

	// init kernell
	FG_initKernell();
	
	// add task
	FG_addTask(&task1,1);
	FG_addTask(&task2,2);
	FG_addTask(&task3,3);
	
	// start kernell
	FG_startKernell();
	
}
